/**
 * Filename: AlbumWallAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 15:20
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.BrowseActivity;
import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AlbumWallAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<YNPhoto> datas;
    private boolean select = false;
    public Long stroge;
    private List<Integer> positions=new ArrayList<>();
    public AlbumWallAdapter(Context context, List<YNPhoto> datas, boolean select) {
        this.select = select;
        this.context = context;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_pic_select;
        RelativeLayout ll_item_album_wall;                                    //列表View
        ImageView iv_item_album_wall_avatar;                            //照片墙图片

        public ViewHolder(final View itemView) {
            super(itemView);
            ll_item_album_wall = (RelativeLayout) itemView
                    .findViewById(R.id.ll_item_album_wall);
            iv_item_album_wall_avatar = (ImageView) itemView
                    .findViewById(R.id.iv_item_album_wall_avatar);
            iv_pic_select = ((ImageView) itemView.findViewById(R.id.iv_pic_select));

            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    ViewGroup.LayoutParams vlp = itemView.getLayoutParams();
                    vlp.height = itemView.getMeasuredWidth();
                    vlp.width = itemView.getMeasuredWidth();
                    itemView.setLayoutParams(vlp);
                    return true;
                }
            });
        }

        public void bindData(final YNPhoto listItems, final int position) {
            if (listItems != null) {
//                String paths = YNUrlHelper.getWallUrl(context, listItems.getThumbnail());
//                String str1 = StringUtils.repaceImgUrl(paths);
                String thumbnail = listItems.getThumbnail();
//                ImageOptHelper.showFrescoImg(uri, iv_item_album_wall_avatar, ,context);
                ImageOptHelper.showTransfer(thumbnail,iv_item_album_wall_avatar,context);
                Integer Iposition=(Integer) position;
                if(positions.contains(Iposition)){
                    iv_pic_select.setImageResource(R.drawable.pic_select_import);
                }else{
                    iv_pic_select.setImageResource(R.drawable.pic_no_select_import);
                }
                if (select) {
                    iv_pic_select.setVisibility(View.VISIBLE);
                    // 点击列表
                    ll_item_album_wall.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String url = listItems.getUrl();
                            if (SelectedImg.mSelectedImage.contains(url)) {
                                iv_pic_select.setImageResource(R.drawable.pic_no_select_import);
                                SelectedImg.mSelectedImage.remove(url);
                                Integer Iposition=(Integer) position;
                                if (positions.size()!=0&&positions.contains(Iposition)){
                                    positions.remove(Iposition);
                                }
                            }else{
                                iv_pic_select.setImageResource(R.drawable.pic_select_import);
                                SelectedImg.mSelectedImage.add(url);
                                Integer Iposition=(Integer) position;
                                if (!positions.contains(Iposition)){
                                    positions.add(Iposition);
                                }
                            }
                        }
                    });
                } else {
                    // 点击列表
                    ll_item_album_wall.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, BrowseActivity.class);
                            intent.putExtra(CommonConstants.YNPOSITION, position);
                            intent.putExtra(CommonConstants.YNDATAS, true);
                            intent.putExtra(CommonConstants.YNQNURL, (Serializable) datas);
                            intent.putExtra("type", "wall");
                            context.startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    private YNPhoto getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_album_wall2, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position) {
        if (rv_holder instanceof ViewHolder) {
            final ViewHolder holder = (ViewHolder) rv_holder;
            YNPhoto listItems = getItem(position);
            holder.bindData(listItems, position);
            holder.itemView.setTag(listItems);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public int getIdByDirection(boolean isTop) {
        if (isTop) {
            if (datas.size() <= 0) {
                return 0;
            }
            return datas.get(0).getPid();
        } else {
            return datas.get(datas.size() - 1).getPid();
        }
    }

    public void addItemsFromBottom(List<YNPhoto> items) {
        if (items == null)
            return;
        this.datas.addAll(datas.size(), items);
        this.notifyItemRangeInserted(datas.size(), items.size());
    }

    public void addItemsFromTop(List<YNPhoto> items) {
        if (items == null)
            return;
        this.datas.addAll(0, items);
        this.notifyItemRangeInserted(0, items.size());
    }
}
